package edu.mama.college.mapper;

import edu.mama.college.model.UserStudyRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository(value ="UserStudyRecordMapper")
public interface UserStudyRecordMapper {

    @Select("UPDATE user_study_record SET mins = mins +1 where user_id = #{userId} AND course_id = #{courseId} AND lesson_id = #{lessonId} and date = CURDATE()")
    void updateUserStudyRecord(UserStudyRecord userStudyRecord);

    @Select("INSERT INTO user_study_record (user_id,course_id,lesson_id,date) value (#{userId},#{courseId},#{lessonId},CURDATE())")
    void insertUserStudyRecord(UserStudyRecord userStudyRecord);

    @Select("SELECT IFNULL(SUM(mins),0) mins FROM user_study_record WHERE user_id = #{userId} AND date = CURDATE()")
    int todayStudiedMins(int userId);

    @Select("SELECT IFNULL(COUNT(DISTINCT(date)),0) days  FROM user_study_record WHERE user_id = #{userId} ")
    int totalStudiedDays(int userId);

    @Select("SELECT IFNULL(COUNT(DISTINCT(course_id)),0) courses_count FROM user_study_record WHERE user_id = #{userId} ")
    int studiedCourses(int userId);

    void scheduleAdd(List<UserStudyRecord> userStudyRecordList);
}
